const Controller = require('../prototype/Controller')
const { models } = require('../model')
const { ObjectID } = require('mongodb')
const msgRobotModel = models.MsgRobot
const { initSchedules } = require('../service/msg/schedule')

/**
 * @type {Object}
 * @example
 * [{
 *     rule: {second:1},
 *     message: '每分钟循环',
 *     job: null,
 * }]
 */

const defaultSchedule = {
  rule: {
    second: null,
    minute: null,
    hour: null,
    date: null,
    year: null,
    dayOfWeek: null
  },
  message: '默认消息'
}

class MsgRobotController extends Controller {
  constructor () {
    super()
    this.model = models.MsgRobot
  }

  findAll () {
    return async (ctx) => {
      const { response, _user } = ctx
      const { group } = this._getRequestQuery(ctx)
      const findQuery = {
        user_id: _user._id
      }
      if (group) {
        findQuery.group = group
      }
      const schedules = await msgRobotModel.coll.find(findQuery).toArray()
      response.wrapper.succ(schedules)
    }
  }

  create () {
    return async (ctx) => {
      const { request, response, _user } = ctx
      const body = request.body
      const schedule = this._formatSchedule(body)
      schedule.user_id = _user._id
      // 自定义分组
      if (body.group) {
        schedule.group = body.group
      }
      const result = await models.MsgRobot.coll.insertOne(schedule)
      await initSchedules()
      const id = result.insertedId
      response.wrapper.succ(id)
    }
  }

  update () {
    return async (ctx) => {
      const { request, response } = ctx
      let _id, body
      if (request.ctx.params.id) {
        _id = ObjectID(request.ctx.params.id)
        body = request.body
      } else {
        const { id, update } = ctx.request.body
        _id = ObjectID(id)
        body = update
      }
      const schedule = this._formatSchedule(body)
      delete schedule._id
      delete schedule.user_id
      const result = await models.MsgRobot.coll.updateOne({ _id }, {
        $set: schedule
      })
      await initSchedules()
      response.wrapper.succ(result)
    }
  }

  deleteById () {
    return async (ctx) => {
      const { request, response } = ctx
      let _id
      if (request.ctx.params.id) {
        _id = ObjectID(request.ctx.params.id)
      } else {
        const { id } = ctx.request.body
        _id = ObjectID(id)
      }
      const result = await models.MsgRobot.coll.deleteOne({ _id })
      await initSchedules()
      response.wrapper.succ(result)
    }
  }

  _formatSchedule (body) {
    // second (0-59)
    // minute (0-59)
    // hour (0-23)
    // date (1-31)
    // month (0-11)
    // year
    // dayOfWeek (0-6) Starting with Sunday
    const schedule = {}
    let rule = null
    if (body.rule) {
      rule = {}
      rule.second = body.rule.second
      rule.minute = body.rule.minute
      rule.hour = body.rule.hour
      rule.date = body.rule.date
      rule.month = body.rule.month
      rule.year = body.rule.year
      rule.dayOfWeek = body.rule.dayOfWeek
    }
    schedule.name = body.name
    schedule.rule = rule || Object.assign(defaultSchedule.rule)
    schedule.message = body.message || defaultSchedule.message
    schedule.ctime = schedule.utime = new Date()
    schedule.webhookUrl = body.webhookUrl
    schedule.secret = body.secret
    schedule.accessToken = body.accessToken
    schedule.feishuWebhookUrl = body.feishuWebhookUrl
    schedule.channels = body.channels
    return schedule
  }
}

// 初始化 schedule
initSchedules().then((msg) => {
  console.log(msg)
})

module.exports = MsgRobotController
